
#include <Rectangle.h>
#include <math.h>

namespace{

const double MIN_RECTANGLE_LENGTH = 0;
const double MAX_RECTANGLE_LENGTH = 100;
const double MIN_RECTANGLE_WIDTH = 0;
const double MAX_RECTANGLE_WIDTH = 75;
const double MIN_SQUARE_SIDE_LENGTH = 0;
const double MAX_SQUARE_SIDE_LENGTH = 75;

double roundDouble(double x)
{
	return floor(x*pow(10, 2)+0.5)/pow(10, 2);
}

}
Rectangle::Rectangle(double length, double width):
	length_m(Side(length, MIN_RECTANGLE_LENGTH, MAX_RECTANGLE_LENGTH, true)),
	width_m(Side(width, MIN_RECTANGLE_WIDTH, MAX_RECTANGLE_WIDTH, false))
{
}

Rectangle::Rectangle(double sideLength):
	length_m(Side(sideLength, MIN_SQUARE_SIDE_LENGTH, MAX_SQUARE_SIDE_LENGTH, false)),
	width_m(Side(sideLength, MIN_SQUARE_SIDE_LENGTH, MAX_SQUARE_SIDE_LENGTH, false))
{
}

double Rectangle::getPerimeter() const
{
   	return (isValid() ? (length_m.getSize()+width_m.getSize())*2 : 0);
}

double Rectangle::getArea() const
{
	return (isValid() ? roundDouble(length_m.getSize()*width_m.getSize()) : 0);
}

bool Rectangle::isValid() const
{
	return (length_m.isSizeValid() && width_m.isSizeValid());
}

void Rectangle::setProperty(double length, double width)
{
	length_m.setSize(length);
	width_m.setSize(width);
}

void Rectangle::setProperty(double sideLength)
{
	length_m.setSize(sideLength);
	width_m.setSize(sideLength);
}

